package com.crm.api.mapper;

import com.crm.api.entity.Employee;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 员工Mapper接口
 */
@Mapper
public interface EmployeeMapper {

    /**
     * 根据用户名查询员工信息
     */
    @Select("SELECT * FROM employee WHERE username = #{username}")
    Employee findByUsername(@Param("username") String username);

    /**
     * 根据ID查询员工信息
     */
    @Select("SELECT * FROM employee WHERE id = #{id}")
    Employee findById(@Param("id") Integer id);

    /**
     * 查询所有员工
     */
    @Select("SELECT * FROM employee ORDER BY id DESC")
    List<Employee> findAll();

    /**
     * 查询员工列表（支持分页和筛选）
     */
    List<Employee> findEmployees(Map<String, Object> params);

    /**
     * 统计员工数量
     */
    Integer countEmployees(Map<String, Object> params);

    /**
     * 插入员工
     */
    void insert(Employee employee);

    /**
     * 更新员工
     */
    void update(Employee employee);

    /**
     * 根据ID删除员工
     */
    void deleteById(Integer id);

    /**
     * 更新员工状态
     */
    void updateStatus(@Param("id") Integer id, @Param("isActive") Boolean isActive, @Param("updatedAt") Date updatedAt);

    /**
     * 获取最大员工编号
     */
    String getMaxEmployeeCode();

    /**
     * 获取所有部门列表
     */
    @Select("SELECT DISTINCT department FROM employee WHERE department IS NOT NULL AND department != '' ORDER BY department")
    List<String> findDistinctDepartments();

    /**
     * 获取所有职位列表
     */
    @Select("SELECT DISTINCT position FROM employee WHERE position IS NOT NULL AND position != '' ORDER BY position")
    List<String> findDistinctPositions();

    /**
     * 检查用户名是否已存在
     */
    Integer existsByUsername(@Param("username") String username);

    /**
     * 检查用户名是否已被其他记录使用
     */
    Integer existsByUsernameAndNotId(@Param("username") String username, @Param("id") Integer id);

    /**
     * 检查员工编号是否已存在
     */
    Integer existsByEmployeeCode(@Param("employeeCode") String employeeCode);

    /**
     * 检查员工编号是否已被其他记录使用
     */
    Integer existsByEmployeeCodeAndNotId(@Param("employeeCode") String employeeCode, @Param("id") Integer id);

    /**
     * 统计关联的业务数量
     */
    Integer countRelatedBusinesses(@Param("id") Integer id);

    /**
     * 统计关联的工单数量
     */
    Integer countRelatedWorkOrders(@Param("id") Integer id);

    /**
     * 更新最后登录时间
     */
    @Update("UPDATE employee SET last_login_time = #{lastLoginTime} WHERE id = #{id}")
    int updateLastLoginTime(@Param("id") Integer id, @Param("lastLoginTime") Date lastLoginTime);

    /**
     * 更新员工密码
     */
    @Update("UPDATE employee SET password = #{password}, updated_at = NOW() WHERE id = #{id}")
    int updatePassword(@Param("id") Integer id, @Param("password") String password);

    /**
     * 更新员工个人资料
     */
    @Update("UPDATE employee SET real_name = #{realName}, gender = #{gender}, phone = #{phone}, " +
            "email = #{email}, updated_at = NOW() WHERE id = #{id}")
    int updateProfile(@Param("id") Integer id, @Param("realName") String realName,
            @Param("gender") String gender, @Param("phone") String phone,
            @Param("email") String email);
}